/**********************************************************************
* Filename: CalendarDate.java
* Created: 2012/04/23
* @author Fernando Brito e Abreu
**********************************************************************/
package org.mgaiao.copaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;

public class CalendarDate
{
	public static Set<CalendarDate> allInstances = new HashSet<CalendarDate>();
	
	private int day;
	private int month;
	private int year;
	
	/**********************************************************************
	* Default constructor
	**********************************************************************/
	public CalendarDate()
	{
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param day the day to initialize
	* @param month the month to initialize
	* @param year the year to initialize
	**********************************************************************/
	public CalendarDate(int day, int month, int year)
	{
		this.day = day;
		this.month = month;
		this.year = year;
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the day of the calendarDate
	**********************************************************************/
	public int day()
	{
		return day;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param day the day to set
	**********************************************************************/
	public void setDay(int day)
	{
		this.day = day;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the month of the calendarDate
	**********************************************************************/
	public int month()
	{
		return month;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param month the month to set
	**********************************************************************/
	public void setMonth(int month)
	{
		this.month = month;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the year of the calendarDate
	**********************************************************************/
	public int year()
	{
		return year;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param year the year to set
	**********************************************************************/
	public void setYear(int year)
	{
		this.year = year;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean dataInitialized(CalendarDate t)
	{
		//	TODO
		return (this.day>=1 && this.day<= 31 && this.month>=1 && this.month<=12 && this.year >= 1900 && this.year <=2200);

	}
	
	/**********************************************************************
	* User-defined operation specified in SOL/OCL
	* @param day the day to set
	* @param month the month to set
	* @param year the year to set
	**********************************************************************/
	public void init(int day, int month, int year)
	{
		//	TODO
		    this.day= day;
		 	this.month = month;
			this.year = year;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param date the date to set
	**********************************************************************/
	public void initS(String date)
	{
		//	TODO
		this.day = Integer.parseInt((date.substring(0,2)));
        this.month = Integer.parseInt((date.substring(3, 5)));
        this.year = Integer.parseInt((date.substring(6, 10)));
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/

	public boolean isAfter(CalendarDate t){
        if(this.year == t.year()){
                if(this.month == t.month()){
                        return this.day >t.day();
                }
                else{
                        return this.month > t.month();
                }
        }
        else{
                return this.year > t.year();
        }
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isBefore(CalendarDate t)
	{
		
		 if(this.year == t.year()){
             if(this.month == t.month()){
                     return this.day <t.day();
             }
             else{
                     return this.month < t.month();
             }
     }
     else{
             return this.year < t.year();
     }
	}
    

	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param x the x to set
	* @param y the y to set
	**********************************************************************/
	public boolean isDivisible(int x, int y)
	{
		//	TODO
		return (((x / y) * y) == x);
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isEqual(CalendarDate t)
	{
		//	TODO
		return (((this.year == t.year) && (this.month == t.month)) && (this.day == t.day));
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	**********************************************************************/
	public boolean isLeap(){
		//	TODO
		 if(this.isDivisible(this.year, 400)){
             return true;
     }
     else{
             if (this.isDivisible(this.year, 100)){
                     return false;
             } 
             else{
                     if (this.isDivisible(this.year, 4)){
                             return true;
                     }
                     else{
                             return false;
                     }
             }
     }
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param date the date to set
	**********************************************************************/
    public void stringToDate(String date){
        String dia, mes,ano;
        dia = date.substring(0,2);
        mes = date.substring(3,5);
        ano = date.substring(6,10);

        this.day=Integer.parseInt(dia);
        this.month =Integer.parseInt(mes);
        this.year = Integer.parseInt(ano);
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public int yearsSince(CalendarDate t)
	{
		//	TODO
		 if((this.month < t.month() || this.month == t.month()) && this.day < t.day()){
         
         return this.year - t.year() - 1;
 }
 else{
         return this.year - t.year();
 }
	}
		
	/* (non-Javadoc)
	* @see java.lang.Object#toString()
	*/
	@Override
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString()
	{
		return "CalendarDate [day=" + day + ", month=" + month + ", year=" + year + "]\n";
	}
	
}
